Imports System.IO
Public Class TempList
    Dim DoDelete As Boolean = False
    Function validatedData(ByVal Author As String, ByVal Subject As String, ByVal Keywords As String) As Boolean
        If Author.Trim = "" Then
            MessageBox.Show("Invalid Author Name", "Virtual Desk", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return False

        End If
        If Subject.Trim = "" Then
            MessageBox.Show("Invalid Document About", "Virtual Desk", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return False

        End If
        If Keywords.Trim = "" Then
            MessageBox.Show("Invalid Documetn Keywords", "Virtual Desk", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return False
        End If

        Return True

    End Function
    Private Sub TempList_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated

    End Sub

    Private Sub TempList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DsTempList.TBL_DOC' table. You can move, or remove it, as needed.
        Me.TBL_DOCTableAdapter.Fill(Me.DsTempList.TBL_DOC)
        Me.TBL_DOC_SUB_CATTableAdapter.Fill(Me.DsTempList.TBL_DOC_SUB_CAT)
        Me.TBL_DOC_CATTableAdapter.FillByDOC_CAT_ACTIVE(Me.DsTempList.TBL_DOC_CAT, True)

        Documents.TblFileTemp.AcceptChanges()
        Me.TempListBindingSource.DataSource = Documents.TblFileTemp
        Me.dgTemplist.DataSource = TempListBindingSource
        TBL_DOCTableAdapter.Fill(Me.DsTempList.TBL_DOC)
    End Sub
    Private Sub BindingNavigatorAddNewItem_Click1(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboRepository.SelectedIndexChanged
        If IsNothing(cboRepository.SelectedValue) = False Then
            Me.TBL_DOC_SUB_CATTableAdapter.FillByActive(Me.DsTempList.TBL_DOC_SUB_CAT, True, cboRepository.SelectedValue)
            Me.txtKeyword.Text = Me.cboRepository.Text
        End If
        

    End Sub

    Private Sub ThemeButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThemeButton1.Click
        Me.Close()
    End Sub

    Private Sub btn_AddToRepository_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_AddToRepository.Click
        Try

            Dim ds As DataRow
            Dim I As Integer
            Dim CLMNo As Integer
            For I = 0 To Documents.TblFileTemp.Rows.Count - 1
                ds = Me.DsTempList.TBL_DOC.NewRow
                For CLMNo = 0 To Documents.TblFileTemp.Columns.Count - 1
                    If Me.validatedData(Documents.TblFileTemp.Rows(I).Item("DOC_AUTHOR"), Documents.TblFileTemp.Rows(I).Item("DOC_SUBJECT"), Documents.TblFileTemp.Rows(I).Item("DOC_KEYWORDS")) = True Then
                        If CLMNo > 0 And CLMNo < 15 Then
                            '' If Documents.TblFileTemp.Columns(CLMNo).Ordinal <> "DOC_PATH" Then
                            ds(CLMNo) = Documents.TblFileTemp.Rows(I).Item(CLMNo)
                            ''End If
                        End If
                    Else
                        Exit Sub
                    End If
                Next
                Me.DsTempList.TBL_DOC.Rows.Add(ds)
                If DoDelete = True Then
                    If File.Exists(Documents.TblFileTemp.Rows(I).Item("DOC_PATH")) = True Then
                        Try
                            File.Delete(Documents.TblFileTemp.Rows(I).Item("DOC_PATH"))
                        Catch ex As Exception
                            MessageBox.Show(ex.Message, "Virtual Desk", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            Err.Clear()
                        End Try

                    End If

                End If
                Me.TBL_DOCTableAdapter.Update(Me.DsTempList.TBL_DOC)
            Next
            Me.DsTempList.TBL_DOC.Merge(Me.DsTempList.TBL_DOC)
            MessageBox.Show("Files Sucessfully Adding to Repositories", "Virtual-Desk", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.DsTempList.TempList.Rows.Clear()
            Documents.TblFileTemp.Clear()
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Virtual-Desk", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Err.Clear()
        End Try
    End Sub

    Private Sub cboSubRepository_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSubRepository.SelectedIndexChanged
        Me.txtKeyword.Text += ", " & Me.cboSubRepository.Text

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Me.TempListBindingSource.EndEdit()
        Me.DsTempList.TempList.Merge(Me.DsTempList.TempList)
        Documents.TblFileTemp.Merge(Documents.TblFileTemp)
        MessageBox.Show("Temporaty List Sucessfully Update." & vbCrLf & "Please Continue With Other Documents.")

    End Sub

    Private Sub chk_delete_source_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk_delete_source.CheckedChanged
        If chk_delete_source.Checked = True Then
            If MessageBox.Show("This Option Delete The Source File. Are Your Sure You Want Delete Source File?", "Virtual Desk", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
                chk_delete_source.Checked = False
                DoDelete = False
            Else
                DoDelete = True
            End If
        End If
    End Sub
End Class